﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;
using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.Support.Hibernate.Manage
{
    /// <summary>
    /// 岗位和菜单关系持久化类
    /// </summary>
    public class PostMenuHibernate
    {
        /// <summary>
        /// 所有字段
        /// </summary>
        /// <param name="prefix">前缀</param>
        /// <returns>所有字段</returns>
        public string Asterisk(string prefix)
        {
            string result = string.Empty;

            result = string.Format("{0}[guid], {0}[insert_user_id], {0}[insert_time], {0}[update_user_id], {0}[update_time], {0}[remark], {0}[validity], {0}[post_id], {0}[menu_id]", prefix);

            return result;
        }

        /// <summary>
        /// 数据解析
        /// </summary>
        /// <param name="values">数据</param>
        /// <returns>岗位和菜单关系</returns>
        public PostMenu Parse(object[] values)
        {
            PostMenu result = new PostMenu();

            try
            {
                result = DatabaseHibernate.ParseModule(result, values) as PostMenu;

                result.PostId = DatabaseHibernate.ParseString(values[7]);
                result.MenuId = DatabaseHibernate.ParseString(values[8]);
                result.PostName = DatabaseHibernate.ParseString(values[9]);
                result.CatalogName = DatabaseHibernate.ParseString(values[10]);
                result.MenuName = DatabaseHibernate.ParseString(values[11]);
                result.MenuUrl = DatabaseHibernate.ParseString(values[12]);
                result.MenuParameter = DatabaseHibernate.ParseString(values[13]);
            }
            catch (Exception exception)
            {
                result = null;
                EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
            }

            return result;
        }

        /// <summary>
        /// 数据解析
        /// </summary>
        /// <param name="values">数据集合</param>
        /// <returns>岗位和菜单关系集合</returns>
        public List<PostMenu> Parse(List<object[]> values)
        {
            List<PostMenu> results = new List<PostMenu>();

            if (values != null)
            {
                for (int i = 0; i < values.Count; i++)
                {
                    PostMenu value = this.Parse(values[i]);
                    results.Add(value);
                }
            }

            return results;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页行数</param>
        /// <param name="total">总数</param>
        /// <returns>岗位菜单关系集合</returns>
        public List<PostMenu> Query(int page, int rows, ref int total)
        {
            List<PostMenu> results = new List<PostMenu>();

            string sql = string.Format("select {0}, [pt].[name] as post_name, [mt].[catalog_name], [mt].[name] as menu_name, [mt].[url] as menu_url, [mt].[parameter] as menu_parameter from m_post_menu as pmt, m_post as pt, (select mt_a.*, [ct_a].[name] as catalog_name, [ct_a].[serial] as catalog_serial from m_menu as mt_a left join m_catalog as ct_a on [mt_a].[catalog_id] = [ct_a].[guid]) as mt where [pmt].[post_id] = [pt].[guid] and [pmt].[menu_id] = [mt].[guid] order by [pt].[name], [mt].[catalog_serial], [mt].[catalog_name], [mt].[serial], [mt].[url], [mt].[name]", this.Asterisk("[pmt]."));
            List<Parameter> parameters = new List<Parameter>();
            DatabaseHibernate hibernate = new DatabaseHibernate();

            string countSql = string.Format("select count(*) from ({0})", sql);
            parameters.Clear();

            List<object[]> values = hibernate.Read(Variable.Link, countSql, parameters);

            if ((values != null) && (values.Count == 1))
            {
                object[] moduleValues = values[0];
                total = DatabaseHibernate.ParseInt(moduleValues[0]);
            }

            string querySql = sql;

            parameters.Clear();

            values = hibernate.Read(Variable.Link, querySql, parameters, page, rows);

            results = this.Parse(values);

            return results;
        }

        /// <summary>
        /// 通过岗位查询
        /// </summary>
        /// <param name="postGuid">岗位主键</param>
        /// <returns>岗位和菜单关系集合</returns>
        public List<PostMenu> QueryByPost(string postGuid)
        {
            List<PostMenu> results = new List<PostMenu>();

            string sql = string.Format("select {0}, [pt].[name] as post_name, [mt].[catalog_name], [mt].[name] as menu_name, [mt].[url] as menu_url, [mt].[parameter] as menu_parameter from m_post_menu as pmt, m_post as pt, (select mt_a.*, [ct_a].[name] as catalog_name, [ct_a].[serial] as catalog_serial from m_menu as mt_a left join m_catalog as ct_a on [mt_a].[catalog_id] = [ct_a].[guid]) as mt where [pmt].[post_id] = [pt].[guid] and [pmt].[menu_id] = [mt].[guid] and [pt].[guid] = :postGuid order by [pt].[name], [mt].[catalog_serial], [mt].[catalog_name], [mt].[serial], [mt].[url], [mt].[name]", this.Asterisk("[pmt]."));
            List<Parameter> parameters = new List<Parameter>();

            parameters.Add(new Parameter("postGuid", DatabaseHibernate.Parameter(postGuid)));

            DatabaseHibernate hibernate = new DatabaseHibernate();

            List<object[]> values = hibernate.Read(Variable.Link, sql, parameters);

            results = this.Parse(values);

            return results;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(PostMenu value)
        {
            bool result = false;

            string sql = string.Format("insert into m_post_menu ({0}) values (:guid, :insert_user_id, :insert_time, :update_user_id, :update_time, :remark, :validity, :post_id, :menu_id)", this.Asterisk(""));
            List<Parameter> parameters = new List<Parameter>();

            parameters.Add(new Parameter("guid", DatabaseHibernate.Parameter(DatabaseHibernate.GUID())));
            parameters.Add(new Parameter("insert_user_id", DatabaseHibernate.Parameter(value.InsertUserId)));
            parameters.Add(new Parameter("insert_time", DatabaseHibernate.Parameter(value.InsertTime)));
            parameters.Add(new Parameter("update_user_id", DatabaseHibernate.Parameter(value.UpdateUserId)));
            parameters.Add(new Parameter("update_time", DatabaseHibernate.Parameter(value.UpdateTime)));
            parameters.Add(new Parameter("remark", DatabaseHibernate.Parameter(value.Remark)));
            parameters.Add(new Parameter("validity", DatabaseHibernate.Parameter(value.Validity)));

            parameters.Add(new Parameter("post_id", DatabaseHibernate.Parameter(value.PostId)));
            parameters.Add(new Parameter("menu_id", DatabaseHibernate.Parameter(value.MenuId)));

            DatabaseHibernate hibernate = new DatabaseHibernate();

            result = hibernate.Write(Variable.Link, sql, parameters);

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="guid">主键</param>
        /// <returns>结果</returns>
        public bool Delete(string guid)
        {
            bool result = false;

            string sql = string.Format("delete from m_post_menu as t where [t].[guid] = '{0}'", guid);
            List<Parameter> parameters = new List<Parameter>();

            DatabaseHibernate hibernate = new DatabaseHibernate();

            result = hibernate.Write(Variable.Link, sql, parameters);

            return result;
        }

        /// <summary>
        /// 通过菜单删除
        /// </summary>
        /// <param name="menuGuid">菜单主键</param>
        /// <returns>结果</returns>
        public bool DeleteByMenu(string menuGuid)
        {
            bool result = false;

            string sql = string.Format("delete from m_post_menu as t where [t].[menu_id] = '{0}'", menuGuid);
            List<Parameter> parameters = new List<Parameter>();

            DatabaseHibernate hibernate = new DatabaseHibernate();

            result = hibernate.Write(Variable.Link, sql, parameters);

            return result;
        }
    }
}
